package com.cty.servlet;

import com.cty.pojo.PageInfo;
import com.cty.service.UserService;
import com.cty.service.impl.UserServiceImpl;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: cty
 * @Date: 2020/3/3 12:53
 * @Description:
 * @version: 1.0
 */
@WebServlet(name = "UserServlet", urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    // 获取日志对象
    Logger log = Logger.getLogger(UserServlet.class);

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求数据
        String pageSizeStr = req.getParameter("pageSize");
        int pageSize = 2;
        if(pageSizeStr!=null && !pageSizeStr.equals("")){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        String pageNumStr = req.getParameter("pageNum");
        int pageNum = 1;
        if(pageNumStr!=null && !pageNumStr.equals("")){
            pageNum = Integer.parseInt(pageNumStr);
        }

        // 获取分页数据
        UserService userService = new UserServiceImpl();
        PageInfo pageInfo = userService.getPageData(pageSize, pageNum);
        log.info(pageInfo);
        req.setAttribute("pageInfo", pageInfo);

        // 请求转发到显示页面
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }
}
